package hoan.mvc.validator;

import hoan.mvc.form.LoginForm;
import hoan.mvc.model.User;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * @author Horizon Jul 23, 2013 11:04:01 AM
 * 
 */
@Component
public class LoginFormValidator implements Validator{

	public boolean supports(Class<?> arg0) {

		return LoginForm.class.isAssignableFrom(arg0);
	}

	public void validate(Object target, Errors errors) {

		LoginForm loginForm = (LoginForm) target;
		User user = loginForm.getUser();
		if(user == null){
			return;
		}
		
		String userName = user.getUserName();
		if(userName == null || userName.trim().equals("")){
			errors.rejectValue("user.userName", "required.userName");
		}
		
		String password = user.getPassword();
		if(password == null || password.trim().equals("")){
			errors.rejectValue("user.password", "required.userName");
		}
	}
}
